const router = require('koa-router')()
const User = require("./../models/users")
router.prefix('/users');
const defaultNum=10000;


//判断账号是否可以注册
router.get('/checkUserName', async (ctx, next)=> {
  let username = ctx.query.username;
  try{
      let userDoc=await  User.findOne({userName:username});
      if(!!userDoc){
          ctx.body={
            code:300,
            msg:"账号已经注册"
          }
      }else{
          ctx.body={
              code:200,
              msg:"账号可以注册"
          }
      }
  }catch(err){
      ctx.body={
          code:500,
          msg:"服务器发生错误"
      }
  }

})

//用户注册
router.post('/userReg', async (ctx, next)=> {
  let regMsg={
     userName:ctx.request.body.username,
     userPwd:ctx.request.body.password,
    };

  try{
      // 获取到当前又多少用户
      let allUser = await User.find({});
      regMsg.userId=defaultNum+allUser.length+1;
      let userDoc= new User(regMsg);
      let userResult = await userDoc.save();
      if(!!userResult){
      ctx.body={
            state:200,
            msg:"用户注册成功"
          }
      }else{
            ctx.body={
                state:300,
                msg:"用户注册失败"
            }
        }
    }catch(err){
      ctx.body={
          state:400,
          msg:"服务器错误"
      }
      console.log(err.message)
    }


})

//用户登录

router.post('/userLogin',async (ctx,next)=>{
  let userMsg={
    userName:ctx.request.body.username,
    userPwd: ctx.request.body.password,
  }
  try{
    let userDoc=await User.findOne(userMsg);
    if(!!userDoc){
      ctx.session.user={
          userId:userDoc.userId,
          userName:userDoc.userName
      };
        console.log(`111---------${JSON.stringify(ctx.session)}`);
      ctx.body={
        code:200,
        msg:"登录成功"
      }
    }else{
      ctx.body={
          code:300,
          msg:"账号密码错误"
      }
    }
  }catch (err) {
      ctx.body={
          state:400,
          msg:"服务器错误"
      }
      console.log(err.message)
  }
})

//利用session保持会话

router.post('/checkUsre',async (ctx,next)=>{
    console.log(`111---------${JSON.stringify(ctx.session)}`);
    if(!!ctx.session.user){
        ctx.body={
            code:200,
            msg:"",
            result:ctx.session.user.userName
        }
    }else{
        ctx.body={
            code:300,
            msg:"用户未登录",
            result:""
        }
    }
})

// 用户登出操作
router.post('/userLoginOut',async (ctx,next)=>{
    ctx.session.user="";
    ctx.body={
        code:200,
        msg:"用户登出成功",
        result:""
    }


})

//获取用户所有的订单信息
router.post('/getUserOrder',async (ctx,next)=>{
    try{
        let userDoc = await  User.findOne({userId:ctx.session.user.userId});
        if(!!userDoc){
            ctx.body={
                code:200,
                msg:"",
                result:userDoc.orderList,
            }
        }else{
            ctx.body={
                code:300,
                msg:"查找数据库出错",
                result:'',
            }
        }

    }catch(err){
        console.log(err.message);
        ctx.body={
            code:400,
            msg:"服务器错误",
            result:'',
        }
    }
})





//测试

// router.get("/text",async (ctx,next)=>{
//     ctx.body={
//         code:200,
//         msg:"suc"
//     }
//
// })
module.exports = router
